﻿using System;
using Newtonsoft.Json.Linq;

namespace Neo4jConnect
{    
    public class Relationship : Neo4jElement
    {
        public string Type { get; private set; }
        public Uri StartUri { get; private set; }
        public Uri EndUri { get; private set; }

        public override void Save()
        {
            GraphDatabase.SetRelationshipProperties(this);
        }

        protected override void OnReadJson(JToken obj)
        {
            base.SelfUri = new Uri(obj.Value<string>("self"));
            
            foreach (var prop in obj.Value<JObject>("data"))
                this.SetProperty(prop.Key, prop.Value.Value<string>());

            Type = obj.Value<string>("type");
            StartUri = new Uri(obj.Value<string>("start"));
            EndUri = new Uri(obj.Value<string>("end"));            
        }

        public override string ToString()
        {
            return string.Format("e[{0}][{1}-{2}->{3}]", 
                Id, 
                ulong.Parse(StartUri.Segments[StartUri.Segments.Length - 1]),
                Type,
                ulong.Parse(EndUri.Segments[EndUri.Segments.Length - 1]));                
        }

        public override void Delete()
        {
            GraphDatabase.DeleteRelationship(this.Id);
        }
    }
}
